package com.delaphone.fadmin.service;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.jpos.gl.GLException;
import org.jpos.gl.GLSession;
import com.delaphone.fadmin.model.FAdmins;
import java.util.ArrayList;
import org.hibernate.criterion.Projections;

/**
 *
 * @author dongtv
 */
public class AdminServiceImp implements AdminService {

    private Logger log = Logger.getLogger("AdminService");

    public boolean createAdminUser(FAdmins info) {
        GLSession gls = null;
        Transaction tx = null;
        try {
            gls = new GLSession("system");
            tx = gls.beginTransaction();
            gls.session().save(info);
            tx.commit();
            return true;
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return false;
    }

    public FAdmins checkLogin(String userName, String password) {
        GLSession gls = null;
        try {
            gls = new GLSession("system");
            Criteria crit = gls.session().createCriteria(FAdmins.class);
            crit.add(Restrictions.eq("username", userName));
            crit.add(Restrictions.eq("password", password));
            crit.add(Restrictions.eq("active", "yes"));
            crit.add(Restrictions.eq("suspended", "no"));
            return (FAdmins) crit.uniqueResult();
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return null;
    }

    public boolean updateAdminUser(FAdmins info) {
        GLSession gls = null;
        Transaction tx = null;
        try {
            gls = new GLSession("system");
            tx = gls.beginTransaction();
            gls.session().update(info);
            tx.commit();
            return true;
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return false;
    }

    public FAdmins getAdminUser(String username) {
        GLSession gls = null;
        try {
            gls = new GLSession("system");
            Criteria crit = gls.session().createCriteria(FAdmins.class);
            crit.add(Restrictions.eq("username", username));
            return (FAdmins) crit.uniqueResult();
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return null;

    }

    public List<FAdmins> listAdminUser(byte role, int pageNumber, int pageSize) {
        GLSession gls = null;
        try {
            gls = new GLSession("system");
            Criteria crit = gls.session().createCriteria(FAdmins.class);
            if (role > 0) {
                crit.add(Restrictions.eq("role", role));
            }
            if (pageSize > 0 && pageNumber > 0) {
                crit.setMaxResults(pageSize);
                crit.setFirstResult(pageSize * (pageNumber - 1));
            }
            return crit.list();
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return new ArrayList<FAdmins>(0);
    }

    public long findUserRowCount(int role) {
        GLSession gls = null;
        try {
            gls = new GLSession("system");
            Criteria crit = gls.session().createCriteria(FAdmins.class);
            if (role > 0) {
                crit.add(Restrictions.eq("role", role));
            }
            crit.setProjection(Projections.rowCount());
            return (Long) crit.uniqueResult();
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return 0L;
    }
}
